import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly';
import { isInAsyncContext } from '../core/utils';

// 异步块的代码生成器
export const asyncLogGenerators = {
  // 异步日志块生成器
  async_log: function (block: Blockly.Block, generator: any) {
    const text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";

    // 生成异步日志函数
    const asyncLogFunc = generator.provideFunction_(
      'asyncLog',
      `async function ${generator.FUNCTION_NAME_PLACEHOLDER_}(text) {
  await new Promise(resolve => setTimeout(resolve, 100));
  console.log(text);
}`
    );

    // 生成调用代码，使用 await 关键字
    const code = `await ${asyncLogFunc}(${text});\n`;
    return code;
  },
};
